<?php
class ControllerCatalogProduct extends Controller {
	private $error = array();

	public function index() {
		if (isset($this->request->get['product_id'])) {
			$product_id = (int)$this->request->get['product_id'];
		} else {
			$product_id = 0;
		}

		$this->load->model('catalog/product');

		$product_info = $this->model_catalog_product->getProduct($product_id);

		if ($product_info) {
			$this->load->model('tool/image');
			$product_info['image'] = $this->model_tool_image->resize($product_info['image'], $this->config->get('config_image_additional_width'), $this->config->get('config_image_additional_height'));
			$data['images'] = array();

			$results = $this->model_catalog_product->getProductImages($this->request->get['product_id']);

			foreach ($results as $result) {
				$data['images'][] = array(
				    'popup' => $this->model_tool_image->resize($result['image'], 640, 384),
				    'thumb' => $this->model_tool_image->resize($result['image'], $this->config->get('config_image_additional_width'), $this->config->get('config_image_additional_height'))
				);
			}

			$product_info['description'] = html_entity_decode($product_info['description'], ENT_QUOTES, 'UTF-8');


			$data['product'] =$product_info;

			$data['product']['islike'] = $this->model_catalog_product->getProductIsLike($this->request->get['product_id']);

			$data['product_attribute'] = $this->model_catalog_product->getProductAttributes($this->request->get['product_id']);



			$discounts = $this->model_catalog_product->getProductDiscounts($this->request->get['product_id']);

			$data['discounts'] = array();

			foreach ($discounts as $discount) {
			    $data['discounts'][] = array(
			        'product_discount_id' =>$discount['product_discount_id'],
			         'product_id'=>$discount['product_id'],
			        'quantity' => $discount['quantity'],
			        'price'    => $discount['price']
			    );
			}

			$this->model_catalog_product->updateViewed($this->request->get['product_id']);

			$this->load->model('catalog/review');
			$data['reviews'] = array();
			$reviews = $this->model_catalog_review->getReviewsByProductId($this->request->get['product_id']);

			foreach ($reviews as $review){
			    $data['reviews']['metas'][]=array(
			        'author' =>$review['author'],
			        'date_added' =>$review['date_added'],
			        'image' =>$review['image'],
			        'name' =>$review['name'],
			        'price' =>$review['price'],
			        'product_id' =>$review['product_id'],
			        'rating' =>$review['rating'],
			        'review_id' =>$review['review_id'],
			        'text' =>$review['text']
			    );
			}
			$data['reviews']['total']=$this->model_catalog_review->getTotalReviewsByProductId($this->request->get['product_id']);

			//秒杀
		    $data['sepcial']  = $this->model_catalog_product->getProductSpecial($this->request->get['product_id']);

		    //售出量
            $data['product']['salecount'] = $this->model_catalog_product->getProductSaleCount($this->request->get['product_id']);

            $this->response->setDataResult(1,"成功",$data);
		} else {
		    $this->response->setDataResult(0,"没有此商品!",null);
		}
	}

	public function addProductReview(){
	    if (!$this->actionCheck(true,true)) {
	        return;
	    }
	    if ($this->validateReview()) {
	        $this->load->model('catalog/review');
	        $this->model_catalog_review->addReview($this->request->post);
	        $this->response->setDataResult(1,"",null);
	    }
	    else
	    {
	        if (count($this->error)>0) {
	            $error_warning = array_shift($this->error);
	        } else {
	            $error_warning = '';
	        }
	        $this->response->setDataResult(0,$error_warning,null);
	        return;
	    }
	}

	public function review() {
	    if(!isset($this->request->get['customer_id']))
	    {
	        $this->response->setDataResult(0,"缂哄皯鍙傛暟 customer_id",null);
	        return;
	    }

	    if (isset($this->request->get['page'])) {
	        $page = $this->request->get['page'];
	    } else {
	        $page = 1;
	    }

	    if (isset($this->request->get['limit'])) {
	        $limit = $this->request->get['limit'];
	    } else {
	        $limit = $this->config->get('config_product_limit');
	    }

	    $this->response->setDataResult(1,"",$this->getReviewByCustomer($this->request->get['customer_id'], $page, $limit));

	}

	protected function getReviewByCustomer($customer_id,$page, $limit)
	{
	    $this->load->model('catalog/review');
	    $this->load->model('tool/image');


	    $review = array();

	    $review['reviews'] = array();

	    $review_total = $this->model_catalog_review->getTotalReviewsByCustomerId($customer_id);

	    $results = $this->model_catalog_review->getReviewsByCustomerId($customer_id, ($page - 1) * $limit, $limit);

	    foreach ($results as $result) {

	        $review['reviews'][] = array(
	            'id' => $result['review_id'],
	            'author'     => $result['author'],
	            'text'       => nl2br($result['text']),
	            'rating'     => (int)$result['rating'],
	            'customer_image' => $result['image'],
	            'customer_id'     => $result['customer_id'],
	            'date_added' => date($this->config->get('date_format_long'), strtotime($result['date_added']))
	        );
	    }

	    $review['total'] = $review_total;
	    $review['pageIndex'] = $page;
	    $review['pageSize'] = $limit;

	    return $review;
	}

	public function comment() {
	    if (!$this->actionCheck(true,true)) {
	        return;
	    }

	    if(!isset($this->request->post['product_id']))
	    {
	        $this->response->setDataResult(0,"缂哄皯鍙傛暟 product_id",null);
	        return;
	    }

	    if(!isset($this->request->post['order_id']))
	    {
	        $this->response->setDataResult(0,"缂哄皯鍙傛暟 order_id",null);
	        return;
	    }

	    $this->load->model('catalog/product');


	    $product_info = $this->model_catalog_product->getSimpleProduct($this->request->post['product_id']);

	    if($product_info)
	    {
	        if ((utf8_strlen($this->request->post['text']) < 1) || (utf8_strlen($this->request->post['text']) > 1000)) {
	            $this->response->setDataResult(0,"缂哄皯鍙傛暟 text",null);
	            return;
	        }

	        if (empty($this->request->post['rating']) || $this->request->post['rating'] < 0 || $this->request->post['rating'] > 10) {
	            $this->response->setDataResult(0,"缂哄皯鍙傛暟 rating",null);
	            return;
	        }

	        $this->request->post['product_customer_id'] = $product_info['customer_id'];

	        $this->load->model('catalog/review');

	        $this->model_catalog_review->addReview($this->request->post['product_id'], $this->request->post['order_id'], $this->request->post);


	        $this->response->setDataResult(1,"",null);
	    }
	    else
	    {
	        $this->response->setDataResult(0,"缇庨涓嶅瓨鍦�" .$this->request->post['product_id'],null);
	    }


	}

	public function like()
	{
	    if (!$this->actionCheck(true,false)) {
	        return;
	    }

	    if(isset($this->request->get['product_id']))
	    {
	        $this->load->model('catalog/product');
	        $liked = $this->model_catalog_product->likeProduct($this->request->get['product_id']);
	        if($liked)
	        {
	            $this->response->setDataResult(1,"",1);
	        }
	        else
	        {
	            $this->response->setDataResult(1,"",0);
	        }
	        return;
	    }
	    else
	    {
	        $this->response->setDataResult(0,"product_id missing",null);
	    }

	}

	public function want()
	{
	    if (!$this->actionCheck(true,false)) {
	        return;
	    }

	    if(isset($this->request->get['product_id']))
	    {
	        $this->load->model('catalog/product');
	        $this->model_catalog_product->wantsProduct($this->request->get['product_id']);
	        $this->response->setDataResult(1,"",1);
	        return;
	    }
	    else
	    {
	        $this->response->setDataResult(0,"product_id missing",null);
	    }

	}


	protected function validateReview() {
	    if(utf8_strlen(trim($this->request->post['text'])) < 1 )
	    {
	        $this->error['warning'] = '请填写评论';
	    }
	    return !$this->error;
	}
}
